package cn.zxy.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.zxy.exception.BizException;
import cn.zxy.moder.LoginInfo;
import cn.zxy.moder.Student;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController //Handle
@RequestMapping("/api/user")
public class UserController {

    @Value("${token.key}")
    private String tokenKey;

    @PostMapping("/login")
    public String login(@RequestBody LoginInfo loginInfo){

       if (loginInfo.getTel().equals("13721337355") && loginInfo.getPassword().equals("123456")){
           int userId = 108;
           String tel = "13721337355";
           String nickName = "赵生好";

           //生成session

           Map<String,Object> map = new HashMap<>();
           map.put("id",userId);
           map.put("tel",tel.substring(0,4)+"****"+tel.substring(8));
           map.put("nickName",nickName);
           map.put("role",nickName);
           map.put(JWT.EXPIRES_AT,System.currentTimeMillis() + 2*24*60*60*1000);

           String token = JWTUtil.createToken(map, tokenKey.getBytes());



           return token;
       }else {
           throw new BizException(402,"手机号或密码不对");
       }
    }

//    student接口
    @GetMapping("/student") //HandlerMethod
    public Student getStudent(){
        return Student.builder().id(1).name("赵生蚝子").build();
    }
}
